package top.forforever.srb.core.controller.admin;


import com.alibaba.excel.EasyExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.forforever.srb.common.result.R;
import top.forforever.srb.core.pojo.entity.Dict;
import top.forforever.srb.core.service.DictService;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 数据字典 前端控制器
 * </p>
 *
 * @author Mr.Jiang
 * @since 2023-04-29
 */
@RestController
@RequestMapping("/admin/core/dict")
public class AdminDictController {

    @Autowired
    private DictService dictService;

    @PostMapping("/addDictByParentId")
    public R addDictByParentId(@RequestBody Dict dict) {
        dictService.addDictByParentId(dict);
        return R.ok();
    }

    @GetMapping("/getDictInfoById/{id}")
    public R getDictInfoById(@PathVariable Long id) {
        Dict dict = dictService.getDictInfoById(id);
        return R.ok().data("dict",dict);
    }

    @PutMapping("/editDictById")
    public R editDictById(@RequestBody Dict dict) {
        dictService.editDictById(dict);
        return R.ok();
    }

    @DeleteMapping("/deleteById/{id}")
    public R deleteById(@PathVariable Long id) {
        dictService.deleteById(id);
        return R.ok();
    }

    @GetMapping("/getDictListByParentId/{parentId}")
    public R getDictListByParentId(@PathVariable Long parentId) {
        List<Dict> list = dictService.getDictListByParentId(parentId);
        return R.ok().data("list",list);
    }

    @PostMapping("/importData")
    public void importData(@RequestParam("file")MultipartFile file) {
        dictService.importData(file);
    }

    @GetMapping("/exportData")
    public void exportData(HttpServletResponse response) {
        dictService.exportData(response);
    }

}

